import { _decorator, color, Component, Label, Node, Sprite, SpriteFrame } from 'cc';

import { ResourceManager } from '../Core/ResourceManager';
import { EEquipmentType, EItemAffix, ItemQualityColors } from './Item';
import { Equipment } from './Equipment';
import { Core } from '../Core/Core';
import { PicPath } from '../Config/ResourceConfig';
import { EEvent, EGameSystem, EPlayerType } from '../Config/Enum';

const { ccclass, property } = _decorator;

//点击装备格子出现的装备详情页面
@ccclass('EquipmentDetail')
export class EquipmentDetail extends Component {

    ndBG
    ndEquipmentSwitchView
    ndEquipmentItem: Node | null = null;
    lbName:Label
    spIcon: Sprite
    path: string
    affixes:EItemAffix[] = [];
    gems
    gemSlots:number
    ndGems: Node | null = null;
    equipment: Equipment = null!;
    // 强化按钮
    btnStrengthen: Node = null!;
    btnGem: Node = null!;
    lbProfession



    protected onLoad(): void {
        console.log("EquipmentDetail onLoad");
        this.ndBG = this.node.getChildByName("BG");
        this.spIcon = this.node.getChildByName("Board").getChildByName("EquipmentItem").getChildByName("Icon").getComponent(Sprite);
        this.btnStrengthen = this.node.getChildByName("Board").getChildByName("Btn_Strengthen");
        this.btnGem = this.node.getChildByName("Board").getChildByName("Btn_Gem");
        this.ndEquipmentItem = this.node.getChildByName("Board").getChildByName("EquipmentItem");
        this.ndGems = this.node.getChildByName("Board").getChildByName("Gems");
        this.ndEquipmentSwitchView = this.node.parent.parent.getChildByName("EquipmentSwitchView");
        this.lbName = this.node.getChildByName("Board").getChildByName("Name").getComponent(Label)
        this.lbProfession = this.node.getChildByName("Board").getChildByName("Profession").getComponent(Label)
        

    }

    protected onEnable(): void {
        console.log("Core.Instance.currentPlayer in equipment detail", Core.Instance.currentPlayer);
        this.ndBG.on(Node.EventType.TOUCH_END, this.onButtonCloseWindow, this);
        this.equipment = Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.get(Core.Instance.currentEquipmentType);
        console.log("当前装备Core.Instance.currentEquipmentType:", Core.Instance.currentEquipmentType);
        

        // 监听装备变化
        Core.Instance.event.on(EEvent.EquipmentChanged, this.onUpdateEquipmentItem, this);

        this.showDesc();
        this.showIcon();
        this.showAffixes();
        this.showGems()

    }

    protected onDisable(): void {
        this.ndBG.off(Node.EventType.TOUCH_END, this.onButtonCloseWindow, this);
                // 监听装备变化
        Core.Instance.event.off(EEvent.EquipmentChanged, this.onUpdateEquipmentItem, this);
        
    }

    //加载Desc
    showDesc() {
        this.lbName.string = this.equipment.name  || "无名装备";

        // 如果是武器，则显示职业限制
        if (this.equipment.equipmentType == EEquipmentType.Weapon) {
            let profession = this.equipment.profession
            switch(profession){
                case []:
                    this.lbProfession.string = "通用"
                    break;
                default:
                    let s = "适合:"
                    for (const p of profession) {
                        let pro = Core.Instance.getProfessionDesc(p as EPlayerType)
                        s += pro + " "
                    }
                    this.lbProfession.string = s
                    break;

                        
            }
        }else{
            this.lbProfession.string = ""
        }


        //  



        // 加载描述
        this.node.getChildByName("Board").getChildByName("Desc").getComponent(Label).string = this.equipment.desc || "无描述";
    }




    //加载Icon
    showIcon() {
        // 加载图标
        //this.spIcon.spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(this.equipment.icon);
        ResourceManager.Instance.loadRes<SpriteFrame>(this.equipment.icon+"/spriteFrame").then((spriteFrame) => {
            this.spIcon.spriteFrame = spriteFrame;
        }).catch((error) => {
            console.error("加载装备图标失败:", error);
        });
        this.ndEquipmentItem.getChildByName("BG").getComponent(Sprite).color = color().fromHEX(ItemQualityColors[this.equipment.quality]); // 设置背景颜色为白色
    }

    //加载词缀
    showAffixes() {
        // 加载词缀
        this.affixes = Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.get(Core.Instance.currentEquipmentType)?.affixes;
        this.gems = Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.get(Core.Instance.currentEquipmentType)?.gems || [];
        this.gemSlots = Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.get(Core.Instance.currentEquipmentType)?.gemSlots || 0;
        console.log("当前装备的词缀:", this.affixes);
        if (this.affixes && this.affixes.length > 0) {
            // this.node.getChildByName("Board").getChildByName("AffixList").active = true;
            for (let i = 0; i < this.affixes.length; i++) {
                console.log("this.node", this.node);
                const affix = this.affixes[i];
                const affixNode = this.node.getChildByName("Board").getChildByName("Affixes").getChildByName("Label_Affix" + (i + 1));
                affixNode.getComponent(Label).string = affix.description;
                affixNode.active = true; // 确保词缀节点是激活的
                //affixNode.getChildByName("Value").getComponent(Label).string = affix.value.toString();
            }
        }
    }

    //加载宝石
    showGems() {
        // 显示宝石插槽
        for (let i = 0; i < this.gemSlots; i++) {
            const gemNode = this.ndGems.getChildByName("Gem" + (i + 1));
            gemNode.active = true; // 激活宝石插槽节点
            if (this.gems && this.gems[i]) {
                const gem = this.gems[i];
                //gemNode.getChildByName('Gem').getComponent(Sprite).spriteFrame = ResourceManager.Instance.getRes<SpriteFrame>(gem.icon) || null;
                ResourceManager.Instance.loadRes<SpriteFrame>(gem.icon+"/spriteFrame").then((spriteFrame) => {
                    gemNode.getChildByName('Gem').getComponent(Sprite).spriteFrame = spriteFrame;
                }).catch((error) => {
                    console.error("加载宝石图标失败:", error);
                });
            } else {
                gemNode.getChildByName('Gem').getComponent(Sprite).spriteFrame = null; // 没有宝石则不显示图标
            }
        }
    }
    






    onButtonCloseWindow() {
        console.log("点击了BG");
        // 关闭物品详情
        this.node.active = false;

    }

    onButtonOpenForgeView() {
        console.log("点击了强化按钮");
        // 如果没有解锁锻造，提示showTips
        if (!Core.Instance.unlockedSystem.has(EGameSystem.Forge)) {
            Core.Instance.showTips("请先解锁锻造功能");
            return;
        }

        // 打开强化界面
        // 这里可以添加打开强化界面的逻辑
        // 比如显示一个新的节点，或者切换到强化界面
        this.node.getChildByName("Strengthen").active = true;
    }

    onButtonOpenGemView() {
        console.log("点击了宝石按钮");
        // 如果没有解锁宝石镶嵌，提示showTips
        // if (!Core.Instance.unlockedSystem.has(EGameSystem.Gem)) {
        //     Core.Instance.showTips("请先解锁宝石镶嵌功能");
        //     return;
        // }

        // // 打开宝石镶嵌界面
        // // 这里可以添加打开宝石镶嵌界面的逻辑
        // // 比如显示一个新的节点，或者切换到宝石镶嵌界面
        // this.node.getChildByName("Gem").active = true;
    }

    onUpdateEquipmentItem(){
        this.equipment = Core.Instance.players.get(Core.Instance.currentPlayer).equipmentData.equipped.get(Core.Instance.currentEquipmentType);
        console.log("装备更新事件收到");
        this.showDesc();
        this.showIcon();
        this.showAffixes();
        this.showGems();


    }



    onButtonEquipmentSwitch() {
        console.log("onButtonEquipmentSwitch");
        // 切换装备界面
        this.ndEquipmentSwitchView.active = !this.ndEquipmentSwitchView.active; // 切换显示状态
        //Core.Instance.event.emit(EEvent.SwitchView, Context.ArsenalView);
        //Core.Instance.event.emit(EEvent.SwitchView, Context.PlayerView);
    }




}


